package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.AdminMapper;
import com.example.mybatisplus.mapper.StudentMapper;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.model.domain.Tea;
import com.example.mybatisplus.service.AdminService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.StudentService;
import com.example.mybatisplus.model.domain.Student;

import java.util.ArrayList;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author yp
 * @since 2021-10-23
 * @version v1.0
 */
@Controller
@RequestMapping("/api/student")
public class StudentController {

    private final Logger logger = LoggerFactory.getLogger( StudentController.class );

    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private AdminMapper adminMapper;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Student  student =  studentService.getById(id);
        return JsonResponse.success(student);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        studentService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateStudent(Student  student) throws Exception {
        studentService.updateById(student);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Student
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Student  student) throws Exception {
        studentService.save(student);
        return JsonResponse.success(null);
    }


    /**
     * 个人中心个人信息的查询
     * @return
     */
    @PostMapping("/stuThing")
    @ResponseBody
    public JsonResponse stuThing(){
        Student student = studentMapper.selectByLoginId(SessionUtils.getCurrentUserInfo().getId());
        return JsonResponse.success(student);
    }
    /**
     * 根据登录学生账号的账号修改基本信息
     */
    @PostMapping("/stuChange")
    @ResponseBody
    public JsonResponse stuChange(@RequestBody Student student){
        studentMapper.updateByLoginId(SessionUtils.getCurrentUserInfo().getId(),student);
        return JsonResponse.success(null);
    }

    /**
     * 学生信息管理获取基本信息
     */

    @GetMapping("/getStuList")
    @ResponseBody
    public JsonResponse list(){
        List<Student> list = studentService.list();
        return JsonResponse.success(list);
    }

    /**
     *
     * @param student
     * @return
     * 学院端学生信息修改
     */
    @PostMapping("/updatastu")
    @ResponseBody
    public JsonResponse updateStu(@RequestBody Student student){
        studentMapper.updateByStuNumber(student.getStuNumber(),student);
        return JsonResponse.success(null);
    }

    /**
     *
     * @param student
     * @return
     * 学院端学生信息删除
     */
    @PostMapping("/deleteStu")
    @ResponseBody
    public JsonResponse deleteStu(@RequestBody Student student){
        studentMapper.deleteByStuNumber(student.getStuNumber());
        return JsonResponse.success(null);
    }
    /**
     * 学院端查询学生信息
     */
    @GetMapping("/stuList")
    @ResponseBody
    public JsonResponse getpageStu(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Student> page = new Page<>(pageNo, pageSize);
        page = studentService.page(page);
        return JsonResponse.success(page);
    }

    /**
     * 学院端对学生信息得模糊查询
     */
    @PostMapping("/selectionWithAdminStu")
    @ResponseBody
    public JsonResponse selectionWithAdminStu(@RequestBody Student student){
        List<Student> students = studentService.selectByStudent(student);
        return JsonResponse.success(students);
    }
    /**
     *学院端对学生得添加
     */
    @PostMapping("/insertStudent")
    @ResponseBody
    public JsonResponse insertStudent(@RequestBody Student student){
        Admin admin=new Admin();
        admin.setLoginName(student.getStuNumber());
        admin.setName(student.getStuName());
        admin.setRemark(1L);
        admin.setPassword(student.getStuNumber().substring(student.getStuNumber().length()-6));
        adminMapper.insert(admin);
        Admin admin1 = adminMapper.selectName(admin.getLoginName());
        student.setAdId(admin1.getId());
        studentMapper.insert(student);
        return  JsonResponse.success(null);
    }
}

